package com.example.administrator.ceshi1230;

import android.app.Activity;


import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class MainActivity extends Activity {
EditText input;
TextView show;
Button send;
Handler handler;
ClientThread clientThread;
    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       input=findViewById(R.id.input);
       send=findViewById(R.id.send);
       show=findViewById(R.id.show);
       handler=new Handler(){
           @Override
           public void handleMessage(Message msg){
               if (msg.what==0x123){
                   show.append("\n"+msg.obj.toString());


               }
           }

       };
       clientThread=new ClientThread(handler);
       new Thread(clientThread).start();
       send.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               try {
                   Message msg=new Message();
                   msg.what=0x345;
                   msg.obj=input.getText().toString();
                   clientThread.revHandler.sendMessage(msg);
                   input.setText("");
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
       });

    }



}
